package com.xuecheng.test.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/***
 * 路由工作模式
 */
public class Producer04_topics {
    //指定队列和交换机以及routingkey
    private static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    private static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    private static final String EXCHANGE_TOPICS_INFROM = "exchange_topics_infrom";
    private static final String ROUTINGKEY_EMAIL = "inform.#.email.#";
    private static final String ROUTINGKEY_SMS = "inform.#.sms.#";

    public static void main(String[] args) {
        //创建连接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");

        Connection connection = null;
        Channel channel = null;
        try {
            //建立连接
            connection = connectionFactory.newConnection();
            //获取通道
            channel = connection.createChannel();
            //声明队列
            channel.queueDeclare(QUEUE_INFORM_EMAIL,true,false,false,null);
            channel.queueDeclare(QUEUE_INFORM_SMS,true,false,false,null);
            //声明交换机
            /**交换机类型:
             *  fanout对应mq的publish/subscribe
             *  direct对应routing
             *  topic对应topics
             *  header对应headers
             *
             */
            channel.exchangeDeclare(EXCHANGE_TOPICS_INFROM, BuiltinExchangeType.TOPIC);

            //交换机和队列进行绑定
            channel.queueBind(QUEUE_INFORM_EMAIL,EXCHANGE_TOPICS_INFROM,ROUTINGKEY_EMAIL);
            channel.queueBind(QUEUE_INFORM_SMS,EXCHANGE_TOPICS_INFROM,ROUTINGKEY_SMS);

            //发送消息
            for (int i=0;i<5;i++){
                String message = "send email msg to usr"+i;
                channel.basicPublish(EXCHANGE_TOPICS_INFROM,"inform.email",null,message.getBytes());
                System.out.println("send to mq :"+message);
            }
            for (int i=0;i<5;i++){
                String message = "send sms msg to usr"+i;
                channel.basicPublish(EXCHANGE_TOPICS_INFROM,"inform.sms",null,message.getBytes());
                System.out.println("send to mq :"+message);
            }
            for (int i=0;i<5;i++){
                String message = "send sms and email msg to usr"+i;
                channel.basicPublish(EXCHANGE_TOPICS_INFROM,"inform.email.sms",null,message.getBytes());
                System.out.println("send to mq :"+message);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(null != channel){
                try {
                    channel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    e.printStackTrace();
                }
            }
            if(null != connection){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
